﻿// https://github.com/NRatel/NRFramework.UI

using System;
using System.Collections.Generic;
using UnityEngine;

namespace NRFramework
{
    public partial class UIRoot
    {
        public string rootId;
        public int startOrder;
        public int endOrder;

        public Dictionary<string, UIPanel> PanelDict { private set; get; }

        public UIRoot()
        {
            PanelDict = new Dictionary<string, UIPanel>();
        }

        public virtual T CreatePanel<T>(string panelId, string prefabPath, int sortingOrder) where T : UIPanel
        {
            // Debug.Assert(!panelDict.ContainsKey(panelId));
            // Debug.Assert(sortingOrder >= startOrder && sortingOrder <= endOrder);
            //
            // T panel = Activator.CreateInstance(typeof(T)) as T;
            // panel.Create(panelId, this, prefabPath);
            // panel.SetSortingOrder(sortingOrder);
            // int siblingIndex = GetCurrentSiblingIndex(sortingOrder);
            // panel.SetSiblingIndex(siblingIndex);
            // panelDict.Add(panel.panelId, panel);
            // UIManager.Instance.SetBackgroundAndFocus();
            //
            // return panel;
            throw new NotImplementedException("not implemented");
        }

        public T CreatePanel<T>(string panelId, string prefabPath) where T : UIPanel
        {
            return CreatePanel<T>(panelId, prefabPath, GetIncrementedSortingOrder());
        }

        public T CreatePanel<T>(string prefabPath, int sortingOrder) where T : UIPanel
        {
            return CreatePanel<T>(typeof(T).Name, prefabPath, sortingOrder);
        }

        public T CreatePanel<T>(string prefabPath) where T : UIPanel
        {
            return CreatePanel<T>(typeof(T).Name, prefabPath);
        }

        public virtual void ClosePanel(string panelId, Action onFinish = null)
        {
            throw new NotImplementedException("not implemented");
            // Debug.Assert(panelDict.ContainsKey(panelId));
            //
            // UIPanel panel = panelDict[panelId];
            // panelDict.Remove(panelId);
            // panel.Close(onFinish);
            //
            // UIManager.Instance.SetBackgroundAndFocus();
        }

        public void ClosePanel<T>(Action onFinish = null) where T : UIPanel
        {
            ClosePanel(typeof(T).Name, onFinish);
        }

        // public virtual void DestroyPanel(string panelId)
        // {
        //     throw new NotImplementedException("not implemented");
        //
        //     // Debug.Assert(panelDict.ContainsKey(panelId));
        //     //
        //     // UIPanel panel = panelDict[panelId];
        //     // panelDict.Remove(panelId);
        //     // panel.Destroy();
        //     //
        //     // UIManager.Instance.SetBackgroundAndFocus();
        // }

        // public void DestroyPanel<T>() where T : UIPanel
        // {
        //     DestroyPanel(typeof(T).Name);
        // }

        public virtual void SetPanelVisible(string panelId, bool visible)
        {
            throw new NotImplementedException("not implemented");

            // Debug.Assert(panelDict.ContainsKey(panelId));
            // UIPanel panel = panelDict[panelId];
            // panel.SetVisible(visible);
            //
            // UIManager.Instance.SetBackgroundAndFocus();
        }

        public void SetPanelVisible<T>(bool visible) where T : UIPanel
        {
            SetPanelVisible(typeof(T).Name, visible);
        }

        public UIPanel GetPanel(string panelId)
        {
            return PanelDict[panelId];
        }

        public T GetPanel<T>(string panelId) where T : UIPanel
        {
            return PanelDict[panelId] as T;
        }

        public T GetPanel<T>() where T : UIPanel
        {
            return GetPanel(typeof(T).Name) as T;
        }

        public bool ExistPanel(string panelId)
        {
            return PanelDict.ContainsKey(panelId);
        }

        public bool ExistPanel<T>()
        {
            return ExistPanel(typeof(T).Name);
        }

        public int FindPanelComponent<T>(string panelId, string compDefine, out T comp) where T : Component
        {
            comp = null;
            if (string.IsNullOrEmpty(panelId)) { return FindCompErrorCode.PANEL_ID_IS_NULL_OR_EMPTY; }
            UIPanel panel = GetPanel(panelId);
            return panel.FindComponent<T>(compDefine, out comp);
        }

        public int FindWidgetComponent<T>(string panelId, string[] widgetIds, string compDefine, out T comp) where T : Component
        {
            comp = null;
            if (string.IsNullOrEmpty(panelId)) { return FindCompErrorCode.PANEL_ID_IS_NULL_OR_EMPTY; }
            if (!ExistPanel(panelId)) { return FindCompErrorCode.NOT_EXIST_THIS_PANEL; }
            UIPanel panel = GetPanel(panelId);
            return panel.FindWidgetComponent<T>(widgetIds, compDefine, out comp);
        }

        protected int GetIncrementedSortingOrder()
        {
            UIPanel topPanel = null;
            foreach (KeyValuePair<string, UIPanel> kvPair in PanelDict)
            {
                UIPanel panel = kvPair.Value;
                if (topPanel == null || panel.canvas.sortingOrder > topPanel.canvas.sortingOrder)
                {
                    topPanel = panel;
                }
            }

            return topPanel != null ? (topPanel.canvas.sortingOrder + topPanel.panelBehaviour.thickness + 1) : startOrder;
        }

        protected virtual int GetCurrentSiblingIndex(int sortingOrder)
        {
            throw new NotImplementedException("not implemented");
            // List<UIPanel> panels = UIManager.Instance.FilterPanels((panel) =>
            // { return sortingOrder > panel.canvas.sortingOrder; });
            //
            // return panels.Count;
        }
    }
}